//
//  MDProject.m
//  evidenceGather
//
//  Created by 王继伟 on 13-8-21.
//  Copyright (c) 2013年 无. All rights reserved.
//

#import "MDProject.h"
#import "ProjectVO.h"

 

@implementation MDProject

//数据库插入记录对象
-(NSString *)insertProjectVO:(ProjectVO *) projectVO{
    NSString *status = [NSString stringWithFormat:@"%d", projectVO.status];
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"insert into projects(projcode,projname,status,prvcode,loginuser) values('%@','%@','%d','%@','%@');",projectVO.projcode,projectVO.projname,projectVO.status,projectVO.prvcode,projectVO.loginuser];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL];
    return projectVO.projcode;
}

-(void *)updateProjectVO:(ProjectVO *) projectVO{
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"update projects set projname='%@',status='%d',prvcode='%@',loginuser='%@'  where projcode='%@'" ,projectVO.projname,projectVO.status,projectVO.prvcode,projectVO.loginuser,projectVO.projcode];
    
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL];
    
}

//新增或者更新问题分类信息
-(void )update:(NSArray *) projectVOs{
    if (nil != projectVOs){
        int count=[projectVOs count];
        for (int i=0;i<count;i++) {
            ProjectVO *projectVO= [projectVOs objectAtIndex:i];
            ProjectVO *tempProjVO= [self qryProjectByPk:projectVO.projcode];
            //如果不存在，则新增
            if(tempProjVO==nil){
                [self insertProjectVO:projectVO];
            }else{
                [self updateProjectVO:projectVO];
            }
        }
        
    }
    
}

//根据用户查询项目类别
-(ProjectVO *)qryProjectByPk:(NSString *) projcode{
    NSString *sqlWhere=@" projcode='";
    sqlWhere=[sqlWhere stringByAppendingString:projcode];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    NSMutableDictionary *dic=[self qryProjectBySqlWhere:sqlWhere];
    if(dic!=nil){
        return [dic objectForKey:projcode];
    }
    return nil;
    
}
//根据用户查询项目
-(NSMutableDictionary *)qryProjectByPkUser:(NSString *) pkUser{
    NSString *sqlWhere=@" loginuser='";
    sqlWhere=[sqlWhere stringByAppendingString:pkUser];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    return [self qryProjectBySqlWhere:sqlWhere];
}

//根据省份查询项目
-(NSMutableDictionary *)qryProjectByPkPrvcode:(NSString *) prvcode{
    NSString *sqlWhere=@" prvcode='";
    sqlWhere=[sqlWhere stringByAppendingString:prvcode];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    return [self qryProjectBySqlWhere:sqlWhere];
}

//根据条件查询项目
-(NSMutableDictionary *)qryProjectBySqlWhere:(NSString *) sqlWhere{
    NSMutableDictionary *result = nil;
    if(nil != sqlWhere && [sqlWhere length] > 0)
    {
        NSMutableString *sql = [[NSMutableString alloc]initWithString:@"select rowid, projcode,projname,status,prvcode,loginuser from projects where"];
        [sql appendString:sqlWhere]; 
        //执行查询语句
        NSArray *rs=[super.dbDelegate executeQuery:sql];
        if(nil != rs && [rs count] > 0){
            //如果查询结果不为空，则初始化dictionary，并循环赋值
            result = [[NSMutableDictionary alloc]init];
            for(int i=0;i < [rs count]; i++){
                NSDictionary *dic = [rs objectAtIndex:i];
                //获取附件对应的主键
                NSString *key = [[NSString alloc] initWithData:[dic objectForKey:@"projcode"] encoding:NSUTF8StringEncoding];
                ProjectVO *projectVO = [result objectForKey:key];
                if(nil == projectVO)   //当结果集内无对应的Bug记录时
                {
                    projectVO = [[ProjectVO alloc]init];
                    projectVO.projcode = key;
                    projectVO.projname =[[NSString alloc] initWithData:[dic objectForKey:@"projname"] encoding:NSUTF8StringEncoding];
                    projectVO.status =[[[NSString alloc] initWithData:[dic objectForKey:@"status"] encoding:NSUTF8StringEncoding]intValue];
                    projectVO.prvcode = [[NSString alloc] initWithData:[dic objectForKey:@"prvcode"] encoding:NSUTF8StringEncoding];
                    projectVO.loginuser = [[NSString alloc] initWithData:[dic objectForKey:@"loginuser"] encoding:NSUTF8StringEncoding];
                    projectVO.rowid =[[[NSString alloc] initWithData:[dic objectForKey:@"rowid"] encoding:NSUTF8StringEncoding]intValue];

                    [result setObject:projectVO forKey:key];
                }
            }
        }
    }
    return result;
}

-(void)deleteProjectsWithProjCodes:(NSArray *)projCodes loginUser:(NSString *)loginuser
{
   
    if(nil == projCodes || 0 == [projCodes count])
    {
        return ;
    }
    NSMutableString *projCodeClause = [[NSMutableString alloc] initWithString:@" projcode in("];
    for (NSString *p_code in projCodes) {
        [projCodeClause appendFormat:@"'%@',",p_code];
    }
    NSString * in_clause =  [projCodeClause substringToIndex:[projCodeClause length] - 1];
    in_clause = [in_clause stringByAppendingString:@") "];
    
    //删除项目
    NSMutableString *del_projects = [[NSMutableString alloc] initWithString:@"delete from projects where "];
    [del_projects appendString:in_clause];
    [del_projects appendFormat:@" and loginuser = '%@'",loginuser];
    [super.dbDelegate executeUpdate:del_projects];
   
//    
    [projCodeClause release];
//    [in_clause release];
    return ;
}

@end
